import { fileURLToPath, URL } from 'node:url'
import postcssPxToViewport from 'postcss-px-to-viewport'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'

// https://vitejs.dev/config/
export default defineConfig({
  base: '/chunleibei',
  plugins: [vue()],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  build: {
    rollupOptions: {
      input: {
        // eslint-disable-next-line no-undef
        index: resolve(__dirname, 'index.html'),
        // eslint-disable-next-line no-undef
        visitor: resolve(__dirname, 'manager.html')
      }
    }
  },
  css: {
    postcss: {
      plugins: [
        postcssPxToViewport({
          viewportWidth: 1242,
          viewportHeight: 2208,
          unitPrecision: 5,
          viewportUnit: 'vw',
          selectorBlackList: ['ignore', '.ignore'],
          minPixelValue: 1,
          mediaQuery: false, // 是否在媒体查询中转换px
          exclude: /([/\\])(node_modules)([/\\])/ // 排除node_modules中的文件
        })
      ]
    }
  },
  server: {
    host: '0.0.0.0',
    proxy: {
      '/chunleibei/api': {
        target: 'http://localhost:3000',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/chunleibei\/api/, '')
      }
    }
  }
})
